package com.ruoyi.common.core.mapper.home;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface HomeOrdersMapper {
    /**
     * 查询总订单数
     */
    @Select("SELECT COUNT(1) FROM orders")
    Long selectTotalOrders();

    /**
     * 查询今日订单数
     */
    @Select("SELECT COUNT(1) FROM orders WHERE DATE(create_time) = CURDATE()")
    Long selectTodayOrders();

    /**
     * 根据状态查询总订单数
     */
    @Select("SELECT COUNT(1) FROM orders WHERE status = #{status}")
    Long selectTotalOrdersByStatus(@Param("status") Long status);

    /**
     * 根据状态查询今日订单数
     */
    @Select("SELECT COUNT(1) FROM orders WHERE status = #{status} AND DATE(create_time) = CURDATE()")
    Long selectTodayOrdersByStatus(@Param("status") Long status);

    /**
     * 查询总销售额
     */
    @Select("SELECT COALESCE(SUM(actual_payment), 0) FROM orders")
    Double selectTotalSalesAmount();

    /**
     * 查询今日销售额
     */
    @Select("SELECT COALESCE(SUM(actual_payment), 0) FROM orders WHERE DATE(create_time) = CURDATE()")
    Double selectTodaySalesAmount();

    /**
     * 查询平台月订单量数据
     *
     * @return 包含月份和订单量数据的列表
     */
    List<Map<String, Object>> selectMonthlyOrders();

    /**
     * 查询最近日订单数据
     */
    @Select("SELECT \n" +
            "    order_no AS '订单编号',\n" +
            "    payment_method AS '支付方式',\n" +
            "    CONCAT('¥', actual_payment) AS '订单总额',\n" +
            "    address_id AS '收货地址',\n" +
            "    status AS '订单状态',\n" +
            "    payment_time AS '支付时间',\n" +
            "    notes AS '订单备注',\n" +
            "    COUNT(*) OVER (PARTITION BY DATE(create_time)) AS '当日订单数'  \n" +
            "FROM \n" +
            "    orders\n" +
            "WHERE \n" +
            "    create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)\n" +
            "ORDER BY \n" +
            "    create_time DESC")
    List<Map<String, Object>> selectRecentDailyOrders();

    /**
     * 查询平台订单支付方式分布数据
     *
     * @return 支付方式分布数据列表
     */
    List<Map<String, Object>> selectPaymentMethodDistribution();
}
